package com.js.admin.service.impl;

import com.js.admin.constant.AdminConstant;
import com.js.admin.entity.BaseElement;
import com.js.admin.entity.BaseTenant;
import com.js.admin.entity.BaseUser;
import com.js.admin.mapper.BaseElementMapper;
import com.js.admin.mapper.BaseTenantMapper;
import com.js.admin.mapper.BaseUserMapper;
import com.js.admin.service.BaseElementService;
import com.js.common.service.impl.BizServiceImpl;
import com.js.common.util.BooleanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ygl
 * @create 2018-10-31
 * @DESCRIPTION
 **/
@Service
public class BaseElementServiceImpl extends BizServiceImpl<BaseElement> implements BaseElementService {

    @Autowired
    private BaseElementMapper baseElementMapper;

    @Autowired
    private BaseUserMapper baseUserMapper;

    @Autowired
    private BaseTenantMapper baseTenantMapper;

    /**
     * 获得所有有菜单的资源
     * 即：有menuId的资源
     * @return
     */
    @Override
    public List<BaseElement> getAllElementPermissions() {
        return baseElementMapper.selectAllElementPermissions();
    }

    /**
     * 获取用户可以访问的资源
     * @param userId
     * @return
     */
    //TODO
    @Override
    public List<BaseElement> getAuthorityElementByUserId(String userId) {
        BaseUser user = baseUserMapper.selectByPrimaryKey(userId);
        if(BooleanUtil.BOOLEAN_TRUE.equals(user.getIsSuperAdmin())){
            return baseElementMapper.selectAllElementPermissions();
        }
        BaseTenant tenant = baseTenantMapper.selectByPrimaryKey(user.getTenantId());

        //租户管理员获取应用的按钮和资源
        if (tenant.getOwner().equals(userId)) {
            return baseElementMapper.selectAuthorityElementByTenantId(user.getTenantId(), AdminConstant.RESOURCE_TYPE_VIEW);
        }

        return baseElementMapper.selectAuthorityElementByUserId(userId,AdminConstant.RESOURCE_TYPE_VIEW);
    }
}
